using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections;

using Maps;
using MapsJavascript;
using Utility;

namespace Maps
{

	public class MapControl : System.Web.UI.WebControls.WebControl
	{
		public MapControl() {
			map = CreateMap();
		}

		public Map map;
		public string containerId = "map";

		public Map CreateMap()
		{
			return new Map(containerId);
		}

		public ArrayList GetJavascript()
		{
			ArrayList headers = new ArrayList();
			JavascriptFactory factory = JavascriptFactory.create();
			JavascriptFunction MapJS = factory.createMapFunc(map);
			
			headers.Add(Utility.Html.htjscript(MapJS.globalVariables() + "\n" +
				"function map_initialize() {\n" + 
				MapJS +
				"}\n" +
				"function map_shutdown() {\n" +
				"GUnload();\n" +
				"}\n" +
				"Event.observe(window, 'load', map_initialize);\n" +
				"Event.observe(window, 'unload', map_shutdown);\n"));

			return headers;
		}

		public string Postback()
		{
			JavascriptFactory factory = JavascriptFactory.create();
			string ret = "" + factory.createMapOverlays(map);
			return ret;
		}

		protected override void Render(HtmlTextWriter output)
		{
			output.Write("<div id=\"" + containerId + "\" " +
				"class=\"map " + CssClass + "\" " +
				"style=\"" +
				(Width != 0 ? "width: " + Width + ";" : "") +
				(Height != 0 ? "height: " + Height + ";" : "") +
				"\"></div>\n");
		}

	}
}
